package com.zsj.zsjkotlinmvvm.ui.video.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zsj.zsjkotlinmvvm.FRAGMENT_TAG_REPOSITORY
import com.zsj.zsjkotlinmvvm.R
import com.zsj.zsjkotlinmvvm.data.model.repository.VideoData
import com.zsj.zsjkotlinmvvm.databinding.FragmentRepositoryBinding
import com.zsj.zsjkotlinmvvm.databinding.FragmentVideotitleBinding
import com.zsj.zsjkotlinmvvm.databinding.LayoutErrorBinding
import com.zsj.zsjkotlinmvvm.ui.BaseFragment
import com.zsj.zsjkotlinmvvm.ui.BaseViewModel
import com.zsj.zsjkotlinmvvm.ui.main.activity.MainActivity
import com.zsj.zsjkotlinmvvm.ui.recyclerview.MultiViewTypeDataBindingAdapter
import com.zsj.zsjkotlinmvvm.ui.repository.adapter.RepositoryAdapter
import com.zsj.zsjkotlinmvvm.ui.repository.fragment.RepositoryFragment
import com.zsj.zsjkotlinmvvm.ui.video.activity.VideoPlayActivity
import com.zsj.zsjkotlinmvvm.ui.video.adapter.VideoTitleAdapter
import com.zsj.zsjkotlinmvvm.ui.video.viewmodel.VideoTitleViewModel
import com.zsj.zsjkotlinmvvm.utils.startActivity
import com.zsj.zsjkotlinmvvm.utils.toastShort
import java.util.*

class VideoTitleFragment private constructor()
    : BaseFragment<FragmentVideotitleBinding, VideoTitleViewModel>(), BaseViewModel.Handlers {

    override val layoutRes: Int = R.layout.fragment_videotitle
    override val viewModel by viewModels<VideoTitleViewModel> { viewModelFactory }
    override val transactionTag: String = FRAGMENT_TAG_REPOSITORY

    private lateinit var language: String
    private val adapter = VideoTitleAdapter()
    private var errorView: View? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        language = getLanguageFromArgs()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initUI()
        initObservers()
        initData()
    }

    private fun initUI() {
        with(binding) {
            lifecycleOwner = this@VideoTitleFragment
            viewModel = this@VideoTitleFragment.viewModel
        }
        initRecyclerView()
        initErrorView()
    }

    private fun initRecyclerView() {
        with(binding.rvRepository) {
            layoutManager = LinearLayoutManager(context)
            adapter = this@VideoTitleFragment.adapter
        }
        adapter.setOnItemClickListener(object:MultiViewTypeDataBindingAdapter.OnItemClickListener{
            override fun onItemClick(view: View, position: Int, data: Any) {
                val videoData = data as VideoData
                val bundle = Bundle()
                bundle.putString("url", videoData.playUrl)
                val intent = Intent(context, VideoPlayActivity::class.java)
                intent.putExtras(bundle)
                startActivity(intent)
            }
        })
    }

    private fun initErrorView() {
        binding.vsError.setOnInflateListener { _, inflated ->
            DataBindingUtil.bind<LayoutErrorBinding>(inflated)?.run {
                lifecycleOwner = this@VideoTitleFragment
                viewModel = this@VideoTitleFragment.viewModel
                handlers = this@VideoTitleFragment
            }
        }
    }

    override fun onNavigationIconClick(view: View) {
        super.onNavigationIconClick(view)
        toastShort("11111111111")
    }

    override fun onRetryClick(view: View) {
        super.onRetryClick(view)
        toastShort("222222222222")
    }

    private fun initObservers() {
        viewModel.isShowErrorView.observe(viewLifecycleOwner, Observer {
            handleErrorView(it)
        })
    }

    private fun handleErrorView(isShowErrorView: Boolean) {
        if (isShowErrorView) {
            errorView
                ?.run { visibility = View.VISIBLE }
                ?: binding.vsError.viewStub?.inflate()?.also { errorView = it }
        } else {
            errorView?.visibility = View.GONE
        }
    }

    private fun initData() =
        with(viewModel) {
            getRepositories(language)
            repositories.observe(viewLifecycleOwner, Observer {
                adapter.setItems(it)
            })
        }

    private fun getLanguageFromArgs(): String =
        arguments?.run { getString("EXTRA_LANGUAGE", "") } ?: ""

    companion object {
        fun newInstance(language: String): VideoTitleFragment =
            VideoTitleFragment().apply {
                arguments = Bundle().apply {
                    putString("EXTRA_LANGUAGE", language)
                }
            }
    }

}